# -*- coding: UTF-8 -*-

from common.Common import *
from common.Constants import *
from po.District import District
from po.Province import Province
from po.Specialty import Specialty

logger = Logger.get_logger(__name__)

# 获取所有特产内容
def getSpecialtiesContent(list):
    for province in list:
        # 判断为港澳台地区
        if len(province.district) == 0:
            for s in province.specialty:
                s.content = getSpecialtyContent(s)
        # 判断为非港澳台地区
        else:
            for d in province.district:
                for s in d.specialty:
                    s.content = getSpecialtyContent(s)
        time.sleep(3)
    return list

# 获取特产文章内容
def getSpecialtyContent(specialty):
    bs = getBS(specialty.url)

    div_tag = bs.select("div.f14")

    if len(div_tag) == 0:
        logger.warning("=== %s <%s> 文章没有内容 ===" % (specialty.name, specialty.url))
        return ""
    content = combinaContent(div_tag[0].contents)
    logger.debug("=== 爬取到 %s <%s> 文章内容： %s" % (specialty.name, specialty.url, content))
    return content

def combinaContent(tags):
    content = ""
    if len(tags) == 0:
        return ""
    for tag in tags:
        try:
            content += tag.get_text()
        except Exception as e:
            content += str(tag)
    return content

# 获取所有区域的特产
def getAllDistrictSpecialty(list):
    for province in list:
        # 判断是港澳台
        if len(province.district) == 0:
            province.specialty = getDistrictSpecialties(province)
        for district in province.district:
            district.specialty = getDistrictSpecialties(district)
            logger.info("=== <%s> 区域特产爬取完成" % (district.name))
        logger.info("=== <%s> 省特产爬取完成" % (province.name))
        time.sleep(3)
    return list

# 获取非港澳台单个地区的特产
def getDistrictSpecialties(district):
    list = []
    starturl = combineURL(district.url)
    while True:
        bs = getBS(starturl)
        info_tag = bs.select("#tjtable > tr > td")
        global spe_id_count
        for i in range(len(info_tag)):
            if i % 2 == 0:
                specialty = Specialty()
                specialty.pic = info_tag[i].select("img")[0]['src']
            else:
                a_tag = info_tag[i].select("div > a")
                specialty.id = spe_id_count
                specialty.name = a_tag[0].get_text()
                specialty.url = restore_absolute_href(a_tag[0]['href'], websites['bytravel'])
                specialty.parent_id = district.id
                try:
                    specialty.summary = a_tag[1].parent.get_text().strip()
                except Exception as e :
                    specialty.summary = info_tag[i].select("div")[0].get_text().strip()
                list.append(specialty)
                logger.info("===爬取到文章信息：%s" % (specialty))
            spe_id_count += 1
        page_tag_list = bs.select("#list-page > ul > li")
        try:
            page_tag = page_tag_list[len(page_tag_list) - 1].select("a")[0]
            if "下一页" not in page_tag.get_text():
                logger.info("===已爬取到本模块特产文章最后一页===")
                return list
            starturl = websites['bytravel'] + "/produce/" + page_tag_list[len(page_tag_list) - 1].select("a")[0]['href']
        except Exception as e:
            logger.info("===已爬取到本模块特产文章最后一页===")
            return list

# 获取所有省份和地区
def getAllProvinceAndDistrict(website):
    list = []
    bs = getBS(websites[website])
    pro_tag = bs.select("div.ht")[:-5]
    global pro_id_count
    for tag in pro_tag:
        # 保存省份信息
        province = Province()
        a_tag = tag.select("a")[0]
        province.id = pro_id_count
        province.url = restore_absolute_href(a_tag['href'], websites[website])
        province.name = a_tag.get_text()
        logger.info("=== 爬取到省份信息：%s ===" % (province.__str__()))
        pro_id_count += 1
        province.district = getProvinceDistricts(province, tag)
        list.append(province)
    return list

# 获取单个省份的所属地区
def getProvinceDistricts(province, tag):
    list = []
    global dis_id_count
    while True:
        tag = tag.next_sibling
        try:
            if tag.next_sibling['id'] != 'list110':
                break
        except Exception as e:
            break
        district = District()
        next_a = tag.select("a")[0]
        district.id = dis_id_count
        district.name = next_a.get_text()
        district.url = restore_absolute_href(next_a['href'], websites['bytravel'])
        district.parent_id = province.id
        dis_id_count += 1
        list.append(district)
        logger.info("=== 爬取到 %s 区域信息：%s ===" % (province.name, district.__str__()))
    return list